Easy2Siksha.com
GNDU Queson Paper 2022
BCA 1
st
Semester
PAPER-I: INTRODUCTION TO PROGRAMMING-C
Time Allowed: 3 Hours Maximum Marks: 75
Note: Aempt Five quesons in all, selecng at least One queson from each secon. The
Fih queson may be aempted from any Secon. All quesons carry equal marks
SECTION-A
1. (a) Why C is called middle level language?
(b) What is keyword? List various keywords of C. Write the use of any ve keywords.
2. (a) Explain data types of C.
(b) Which are unformaed Input funcorls?
SECTION-B
3. Compare the following statements of C. Also give an example of each:
(a) while and for
(b) break and connue.
4. Write a program to check if a number given by user is Palindrome or not.
SECTION-C
5. (a) What is a string? Explain any ve string manipulaon funcons.
Easy2Siksha.com
(b) Write a program to get a string from user and count number of vowels in it.
6. What is Recursion? Write a program to nd factorial of a given number using recursive
funcon.
SECTION-D
7. What is structure in C? Which are types of structures ? Explain the uses of structures:
Give an example.
8. What is a pointer ? How are pointers helpful in accessing an array ? Explain
call by reference.
Easy2Siksha.com
GNDU Answer Paper 2022
BCA 1
st
Semester
PAPER-I: INTRODUCTION TO PROGRAMMING-C
Time Allowed: 3 Hours Maximum Marks: 75
Note: Aempt Five quesons in all, selecng at least One queson from each secon. The
Fih queson may be aempted from any Secon. All quesons carry equal marks
SECTION-A
1. (a) Why C is called middle level language?
(b) What is keyword? List various keywords of C. Write the use of any ve keywords.
Ans: 1(a). Why C is called a Middle-Level Language?
Lets begin this explanaon with a short, thoughul story.
󹴮󹴯󹴰󹴱󹴲󹴳 The Tale of the Bridge Builder
Once upon a me, in a vast kingdom of computers, there were two groups of languages
one that spoke the language of humans (like English) and another that whispered in machine
code (just 0s and 1s). These two groups lived far apart, and they couldn’t understand each
other.
Now, many programmers lived in this kingdom. Some liked the human languages (called
high-level languages) because they were easy to use. Others preferred the machine
languages (called low-level languages) because they oered full control over the computer.
But this gap caused problems — the programmers couldn’t easily control the machine
without learning complex binary, and machines couldn’t understand English-like instrucons
directly.
Then came a young and brilliant builder — the C language. It built a bridge between the two
lands. It could talk to both humans and machines! It was powerful like machine language,
and also readable like high-level language.
And thats how C became the middle-level language, standing right between high-level and
low-level languages.
Easy2Siksha.com
󷃆󼽢 Now, Let’s Understand This Technically:
C is called a middle-level programming language because it combines the best features of
both high-level and low-level languages.
Let’s break this down further:
󹻂 High-Level Language Features of C:
These are user-friendly and help the programmer write programs that are easy to read,
write, and understand.
C supports structured programming (using funcons).
It allows use of control statements like if, for, while.
It supports data types like int, oat, char.
You can write portable code, i.e., the same program can run on dierent computers
with lile or no modicaon.
󹻂 Low-Level Language Features of C:
These are close to machine-level instrucons and provide more control over the hardware.
It allows direct memory access using pointers.
You can write system-level programs like operang systems and device drivers in C.
It supports bitwise operaons, memory management, etc.
󹻂 Hence, C is "Middle-Level":
Because it gives the power of low-level programming and the ease of high-level
programming — all in one language.
Examples of What You Can Do with C:
Feature
Example Use
High-level: Funcons
int sum(int a, int b) { return a + b; }
Low-level: Pointers
int *ptr = #
High-level: Loops
for (int i = 0; i < 10; i++)
Low-level: Memory management
malloc(), free()
Mixed: Bitwise operaons
a = a & b;
󹰤󹰥󹰦󹰧󹰨 Summary (1a):
Easy2Siksha.com
C is called a middle-level language because:
It provides the eciency and control of low-level languages (like assembly),
As well as the simplicity and structure of high-level languages (like Python, Java),
Thus, it acts as a bridge between machine and human-readable code.
1(b). What is a Keyword?
List various keywords of C.
Write the use of any ve keywords.
󽄡󽄢󽄣󽄤󽄥󽄦 Denion: What is a Keyword?
In simple terms:
A keyword is a special word that has a xed meaning in the C language and cannot be used
for anything else like variable names.
They are the building blocks of C programs.
Think of them as the grammar rules of the language. Just like in English, we can’t change the
meaning of “if” or “but, in C, we can’t change the meaning of keywords like if, return, or
while.
󼪺󼪻 How Many Keywords Are There in C?
C language has 32 standard keywords (in tradional ANSI C). Some compilers add more, but
the core set is 32.
Here is the full list:
auto break case char const connue
default do double else enum extern
oat for goto if int long
register return short signed sizeof stac
struct switch typedef union unsigned void
volale while
󷗭󷗨󷗩󷗪󷗫󷗬 Usage of Any Five Keywords (with examples):
Easy2Siksha.com
Lets now explore 5 important keywords and their uses:
1. int
󷃆󼽢 Use:
int is used to declare variables that store integer (whole number) values.
󼨽󼨾󼨿󼩁󼩀 Example:
int age = 25;
This means: "Create a variable called age which can hold integer values."
2. if
󷃆󼽢 Use:
if is used to make decisions in a program. It executes a block of code only if a certain
condion is true.
󼨽󼨾󼨿󼩁󼩀 Example:
if (age > 18) {
prin("You are eligible to vote.");
}
This will print the message only if the value of age is greater than 18.
3. return
󷃆󼽢 Use:
return is used to return a value from a funcon to the calling funcon.
󼨽󼨾󼨿󼩁󼩀 Example:
int add(int a, int b) {
return a + b;
}
This funcon returns the sum of a and b.
4. while
Easy2Siksha.com
󷃆󼽢 Use:
while is used to create a loop that connues to run as long as the condion is true.
󼨽󼨾󼨿󼩁󼩀 Example:
int i = 0;
while (i < 5) {
prin("%d\n", i);
i++;
}
This will print numbers from 0 to 4.
5. struct
󷃆󼽢 Use:
struct is used to group dierent types of variables into a single unit. It is like creang a
custom data type.
󼨽󼨾󼨿󼩁󼩀 Example:
struct Person {
char name[50];
int age;
};
Here, Person is a structure containing a name and age.
󷗛󷗜 Bonus Analogy – A Mini Story for Understanding Keywords:
Let’s imagine C language is a courtroom.
The judge is the C compiler.
Keywords are the laws of the court.
You (the programmer) must follow these laws.
You cannot redene or ignore these laws.
If you try to make your own rule, like int if = 10;, the judge will immediately shout,
“Syntax Error!”
So remember: keywords are sacred in C — they tell the compiler exactly what to do.
Easy2Siksha.com
󼨐󼨑󼨒 Final Summary of Part (b):
Keyword
Purpose
int
Declares an integer variable
if
Used for condional branching
return
Sends a value from a funcon
while
Used for looping while a condion is true
struct
Groups dierent variables into one user-dened type
󹱑󹱒 Important Points:
There are 32 reserved keywords in C.
You cannot use keywords as variable names.
Keywords are case-sensive: If, IF, and if are dierent (only if is valid).
󺚽󺚾󺛂󺛃󺚿󺛀󺛁 Conclusion:
Understanding why C is called a middle-level language gives you a deep appreciaon of its
power — it's the language that blends the machine's logic with the programmer's thoughts.
Learning about keywords helps you use the language properly and eecvely, like knowing
which keys unlock which doors.
By mastering both these concepts, you're not just learning to code — you're learning to
speak to the computer in its own voice.
2. (a) Explain data types of C.
(b) Which are unformaed Input funcorls?
Ans: (a). Once upon a me in a digital kingdom, there lived a wise old compiler who
managed a land full of numbers, characters, and informaon. Every piece of data in the
kingdom had to belong to a specic group so the compiler could manage memory and
operaons eciently. This magical grouping was known as Data Types in the world of C
programming.
Let us now take a guided journey through this data kingdom to understand how each type
works, where it lives, and why it is important.
󷫋󷫌󷫍󷫎󷫏 What Are Data Types in C?
Easy2Siksha.com
Think of data types like labels or containers. In real life, we store water in boles, books on
shelves, and money in wallets — each item has a specic type of container. Similarly, in C
programming, data types dene what kind of data we are storing and how much space it
needs in memory.
Without data types, the computer would be confused — it wouldn’t know whether to treat
a number like an integer, a decimal, a character, or something else.
󹵲󹵳󹵴󹵵󹵶󹵷 Major Categories of Data Types in C
In C, data types are primarily divided into four broad categories:
1. Basic Data Types
2. Derived Data Types
3. Enumeraon Data Types
4. User-Dened Data Types
Lets now explore each category with relatable examples.
1. 󼩕󼩖󼩗󼩘󼩙󼩚 Basic Data Types
These are the foundaon stones of the C language. They include:
a) int (Integer)
This type is used to store whole numbers — like 1, 2, 100, -5, etc.
Example: int age = 25;
Size: Usually 4 bytes
Range: -2,147,483,648 to 2,147,483,647 (on most systems)
The int type is like a locker that only stores whole things — no fracons allowed!
b) oat (Floang Point)
Used for storing decimal values like 2.5, 3.1415, -0.99, etc.
Example: oat price = 99.99;
Size: 4 bytes
Precision: Up to 6 digits aer the decimal
You can think of a oat like a weighing scale — its perfect when precision maers but
doesn’t need to be exact.
c) double (Double Precision Floang Point)
Easy2Siksha.com
When oat is not enough, double steps in.
Example: double pi = 3.14159265359;
Size: 8 bytes
Precision: Up to 15 digits aer the decimal
Its like a scienc calculator — more space and more accuracy.
d) char (Character)
This stores a single character like 'A', 'b', '#', '9', etc.
Example: char grade = 'A';
Size: 1 byte
Behind the scenes, char uses ASCII values. For example, 'A' is stored as 65.
󷉃󷉄 Story Time: The Tale of the Mismatched Variable
Once, a young programmer named Riya was building a calculator in C. She wrote:
int result = 5.5;
But when she printed the result, it was just 5!
The compiler whispered, "You asked me to store a decimal number in an integer box so I
chopped o the decimal!"
Moral of the story? Always choose the right data type to avoid surprises!
2. 󼨻󼨼 Derived Data Types
These are created using basic data types. They include:
a) Array
A collecon of elements of the same type.
Example: int marks[5] = {90, 85, 78, 92, 88};
Think of an array as a row of mailboxes, each holding a value of the same type.
b) Pointer
Stores the memory address of another variable.
Example: int *ptr = &age;
Pointers are like GPS trackers — they don’t hold the data but show where the data lives.
Easy2Siksha.com
c) Funcon
In C, funcons are also considered derived data types because they work with data types as
return types and arguments.
3. 󼩕󼩖󼩗󼩘󼩙󼩚 Enumeraon Data Types (enum)
These are user-dened types consisng of named integer constants. They make the code
more readable.
enum week { Monday, Tuesday, Wednesday, Thursday, Friday };
Now instead of wring numbers 0 to 4, you can use meaningful names like Monday or
Friday.
Behind the scenes: Monday = 0, Tuesday = 1, and so on.
4. 󼩣󼩤󼩥󼩦󼩧󼩨󼩩 User-Dened Data Types
C allows you to create your own data types using:
a) struct (Structure)
Groups dierent data types into one unit.
struct Student {
int roll;
char name[50];
oat marks;
};
Now you can create a single variable to hold a student’s roll number, name, and marks.
b) union
Like struct, but with a memory-saving twist — all members share the same memory locaon.
union Data {
int i;
oat f;
char str[20];
};
Only one value can be stored at a me, making it memory ecient.
Easy2Siksha.com
c) typedef
Used to create a new name (alias) for an exisng data type.
typedef unsigned int uint;
uint age = 30;
Its like giving someone a nickname — easier to use and remember.
󼿍󼿎󼿑󼿒󼿏󼿓󼿐󼿔 Signed vs Unsigned Data Types
Every integer or character can be either signed or unsigned.
Signed: Can store both posive and negave values.
Unsigned: Only stores posive values (extends the upper limit).
unsigned int x = 300; // no negaves allowed!
󼨐󼨑󼨒 Memory Size Summary (May vary depending on system)
Data Type
Size (in bytes)
Example Value
char
1 byte
'A'
int
4 bytes
100
oat
4 bytes
3.14
double
8 bytes
3.14159265
short
2 bytes
32767
long
8 bytes
123456789
󼨽󼨾󼨿󼩁󼩀 Final Example to Show Importance of Data Types
#include <stdio.h>
int main() {
int age = 20;
oat height = 5.9;
char grade = 'A';
Easy2Siksha.com
prin("Age: %d\n", age);
prin("Height: %.1f\n", height);
prin("Grade: %c\n", grade);
return 0;
}
Here, each variable is declared with the proper data type — making the code clean,
readable, and ecient.
󷙎󷙐󷙏 Conclusion: Why Data Types Maer?
In the kingdom of C, data types are the laws of the land. They ensure that:
The right amount of memory is used.
Operaons are performed correctly.
The code remains bug-free and easy to understand.
So, whether youre building a calculator, a game, or an operang system — remember:
Choose your data types wisely. They are the invisible foundaon upon which every C
program stands.
(b) Which are unformaed Input funcorls?
Ans: 󼏨󼏩󼏪󼏫󼏬󷸓󼏭󼏮󷸕󼏯󷸖󼏰󼏱󼏲󼏳󼏴 The Curious Kid and the Magic Box
Once upon a me, there was a curious lile boy named Aarav. He had a magical talking box
that could take instrucons and give answers. But this box had two ways of understanding
instrucons:
1. One way was very strict – it expected complete details, well-arranged and properly
formaed input. If Aarav missed even a ny detail, the box would give an error.
2. The second way was friendly and exible – it didn’t care much about format. It just
took the input as it came, without bothering about spaces, structure, or extra rules.
This friendly way is exactly what unformaed input funcons in programming are like!
󺂌󺂍󺂎󺂏󺂐 Now Let’s Come to the Real Programming World
Easy2Siksha.com
In programming, especially in languages like C, we need to take input from the user. This
input can be a character, a word, a sentence, or even an enre paragraph.
To take input, we use input funcons. These funcons are mainly of two types:
1. Formaed Input Funcons – e.g., scanf()
These are strict. They need proper format speciers like %d, %c, %s to work.
2. Unformaed Input Funcons – These are simple and take input without caring about
formats or types.
󷃆󼽢 What Are Unformaed Input Funcons?
Unformaed input funcons are funcons in C (and similar languages) that read input
without any formang. They do not use format speciers and are mainly used for reading
single characters, strings, or raw data from the standard input (like keyboard).
These funcons are simple to use, but they have some limitaons too.
󹸯󹸭󹸮 Types of Unformaed Input Funcons
Lets explore the common unformaed input funcons in C:
1. getchar()
󹵭󹵮󹵯󹵱 Use: Reads a single character from the standard input (usually the keyboard).
󼨐󼨑󼨒 How it works: It waits for the user to type a character and press Enter. Then it
stores that character.
󷃆󼽢 Example:
char ch;
ch = getchar();
󼪺󼪻 Note: It does not skip white spaces (like space, tab, newline). Whatever you enter rst
will be stored.
2. gets() (Deprecated)
󹵭󹵮󹵯󹵱 Use: Reads a string of characters, including spaces, unl a newline character is
encountered.
󷃆󼽢 Example:
Easy2Siksha.com
char str[100];
gets(str);
󺪸󺪹 Warning: gets() is not safe. It does not check the size of the buer, which can lead to
buer overow.
󷃆󷄕󷄖󷄗 Alternave: Use fgets() instead.
3. getch()
󹵭󹵮󹵯󹵱 Use: Reads a single character without waing for Enter and does not display it on
the screen.
󷃆󼽢 Example:
char ch;
ch = getch();
󹸺󹸻󹸼󹸹 Use case: Useful for password input where you don't want the typed character to be
shown.
4. getche()
󹵭󹵮󹵯󹵱 Use: Reads a single character like getch() but displays the typed character on the
screen.
󷃆󼽢 Example:
char ch;
ch = getche();
󹷝󹷞󹷟󹷣󹷠󹷤󹷥󹷡󹷢 This is helpful when you want to conrm the user pressed the correct key but sll don’t
want to use Enter.
5. fgets()
󹵭󹵮󹵯󹵱 Use: Reads a line of text, including spaces, and stops when the buer is full or
when a newline is entered.
󷃆󼽢 Example:
char str[100];
fgets(str, 100, stdin);
Easy2Siksha.com
󷗭󷗨󷗩󷗪󷗫󷗬 Why beer than gets()? It avoids buer overow by liming the number of characters
you can input.
󹴷󹴺󹴸󹴹󹴻󹴼󹴽󹴾󹴿󹵀󹵁󹵂 Lets Connect This with the Real World Again
Aarav wanted to play a game with his magical box. He just wanted to say, “Hi there! I’m
happy.” But when he used the strict input method (formaed funcon), he had to explain:
"Hi" is a string, "there!" is another, and "I'm happy" is yet another.
But when he used unformaed input funcons like fgets(), he could simply say the full
sentence in one go—and the box understood it. Easy and convenient!
󹰤󹰥󹰦󹰧󹰨 Summary Table for Quick Revision
Funcon
Waits for Enter
Displays Input
Safe to Use?
getchar()
Yes
Yes
Yes
gets()
Yes
Yes
󽅂 No
getch()
No
No
Yes
getche()
No
Yes
Yes
fgets()
Yes
Yes
󷃆󼽢 Yes
󷃆󼽢 Conclusion
Unformaed input funcons are like the easy-going friend of the programmer—they don't
ask too many quesons, they don't care about formats, and they just let you type freely.
They are best when you need to read raw data like characters and strings quickly and simply.
But just like Aarav learned, with great simplicity comes great responsibility. Always choose
the right funcon for the right task, and stay away from risky ones like gets().
Easy2Siksha.com
SECTION-B
3. Compare the following statements of C. Also give an example of each:
(a) while and for
(b) break and connue.
Ans: 󷇴󷇵󷇶󷇷󷇸󷇹 A Dierent Start: The Tale of Two Programmers
Once upon a me in a quiet tech town, there were two young programmers: Anu and Rahul.
Both were learning the C language in college, but each had a unique way of approaching
problems.
Anu was someone who liked to keep things clear and structured, while Rahul enjoyed
exibility and control. One ne day, their professor gave them a challenge:
“Compare the while and for loops in C, and also explain the dierence between break and
connue with suitable examples. Write it as if you’re teaching a curious beginner.
And just like that, the two friends started their journey to understand these concepts deeply.
󼨐󼨑󼨒 PART A: Comparing while and for Loops in C
󷉃󷉄 1. What is a Loop?
Before we dive into the comparison, let’s quickly revise what a loop is.
In programming, a loop is a way to repeat a block of code mulple mes — either a xed
number of mes or unl a condion is met.
Imagine you're doing jumping jacks. You decide to do them 10 mes or unl you're red.
This is exactly how loops work in programming.
C provides several kinds of loops, but here we’ll focus on:
while loop
for loop
󷃆󹸊󹸋 2. while Loop — A Condion-Based Loop
The while loop checks the condion rst. If the condion is true, it enters the loop body.
󷉸󷉹󷉺 Syntax of while loop:
while(condion) {
Easy2Siksha.com
// code to execute
}
󻥪󻥫󻥬󻥭󻥮󻥯󻥰 Example of while loop:
#include <stdio.h>
int main() {
int i = 1;
while(i <= 5) {
prin("Hello %d\n", i);
i++;
}
return 0;
}
󹴮󹴯󹴰󹴱󹴲󹴳 Explanaon:
We start with i = 1
The loop runs as long as i <= 5
Inside the loop, it prints Hello i and then increases i by 1
󹳴󹳵󹳶󹳷 Key Points:
Best when you don’t know how many mes the loop should run.
Entry-controlled: the loop may not run at all if the condion is false at the beginning.
󷃆󹸃󹸄 3. for Loop — A Count-Controlled Loop
The for loop is used when we know in advance how many mes to repeat the block.
󷉸󷉹󷉺 Syntax of for loop:
for(inializaon; condion; increment) {
// code to execute
}
Easy2Siksha.com
󷋥󷋦󷋧󷋨󷋬󷋩󷋪󷋭󷋫 Example of for loop:
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
prin("Hello %d\n", i);
}
return 0;
}
󹴮󹴯󹴰󹴱󹴲󹴳 Explanaon:
Everything (inializaon, condion, and increment) is in one line.
Its compact and easy to read for xed iteraons.
󼩕󼩖󼩗󼩘󼩙󼩚 4. Comparison Between while and for
Feature
while Loop
for Loop
Syntax Style
Spread across mulple lines
All-in-one compact syntax
Use Case
When number of iteraons is
not known
When number of iteraons is
known
Inializaon
Done outside the loop
Done in the loop header
Condion Check
At the beginning
At the beginning
Loop Variable
Update
Inside the loop body
In the loop header
Readability
Slightly less readable for xed
iteraons
More readable for xed number of
iteraons
󷇴󷇵󷇶󷇷󷇸󷇹 Real-Life Analogy
Think of while loop as checking your wallet before buying something — you’ll only proceed if
you have enough money.
while(money >= price) {
Easy2Siksha.com
buy_item();
money -= price;
}
Think of for loop as going to the gym and deciding to do 10 push-ups — you already know
how many mes to do it.
for(int i = 1; i <= 10; i++) {
do_pushup();
}
󹴷󹴺󹴸󹴹󹴻󹴼󹴽󹴾󹴿󹵀󹵁󹵂 Short Story: The Sandwich Shop
Anu wanted to make sandwiches unl she ran out of bread. So she used a while loop.
int bread = 5;
while(bread > 0) {
prin("Made a sandwich.\n");
bread--;
}
Rahul, on the other hand, wanted to make exactly 5 sandwiches no maer what.
for(int i = 0; i < 5; i++) {
prin("Made a sandwich.\n");
}
Both made sandwiches, but the mindset was dierent!
󼨐󼨑󼨒 PART B: Comparing break and connue in C
󺪸󺪹 1. What is break?
The break statement is used to immediately exit the loop or switch block.
When break is executed, the loop stops and the control jumps outside the loop.
󷋣󷋤 Example of break:
Easy2Siksha.com
#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++) {
if(i == 6) {
break;
}
prin("%d ", i);
}
return 0;
}
󹴮󹴯󹴰󹴱󹴲󹴳 Output:
1 2 3 4 5
󷗭󷗨󷗩󷗪󷗫󷗬
Explanaon:
The loop stops as soon as i becomes 6.
It doesn’t complete all 10 iteraons.
󷃆󼼐 2. What is connue?
The connue statement skips the current iteraon and goes to the next one.
It does not exit the loop enrely.
󻐜󻐝󻐞󻐟󻐠󻐡󻐢󻐣󻐤󻐥󻐦󻐧󻐨 Example of connue:
#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++) {
if(i == 6) {
connue;
}
Easy2Siksha.com
prin("%d ", i);
}
return 0;
}
󹴮󹴯󹴰󹴱󹴲󹴳 Output:
1 2 3 4 5 7 8 9 10
󷗭󷗨󷗩󷗪󷗫󷗬 Explanaon:
When i becomes 6, it skips the prin and moves to 7.
The rest of the loop connues normally.
󹸯󹸭󹸮 3. Comparison Between break and connue
Feature
break
connue
Funcon
Exits the loop enrely
Skips the current iteraon
Eect on
Loop
Control jumps outside the loop
Control jumps to the next loop cycle
Common
Use
When a condion is met to stop
completely
When a specic condion should be
ignored
Placement
Usually inside if statements
Usually inside if statements
󷇴󷇵󷇶󷇷󷇸󷇹 Real-Life Analogy
Imagine a movie theatre.
If there's a re alarm, everyone leaves immediately — this is like break.
If someone’s phone rings, they just mute it and connue watching — this is like
connue.
󷻟󷻠󷻡󷻢󻯗󼊺󼊻󻯘󷻭󻯙󻯚󻯛󻯜󼊼󼊽󼊾󼊿󼋀󻯝󻯞󷻮󷻯󷻰󼋁󼋂󼋃󼋄󷻱󷻲󷻳󷻴 Short Story: Cooking Pasta
Anu is boiling pasta for dinner.
If she nds the gas cylinder is empty, she stops cooking immediately — break.
while(true) {
Easy2Siksha.com
if(gasEmpty()) {
break;
}
cookPasta();
}
If she sees one pasta is undercooked, she skips that one and connues boiling the
rest connue.
for(int i = 0; i < 10; i++) {
if(pasta[i] == "undercooked") {
connue;
}
eatPasta(pasta[i]);
}
󷃆󼽢 Final Summary Table
Feature
while Loop
for Loop
break
connue
Usage
Type
Unknown iteraons
Known
iteraons
Exit the loop
Skip current
iteraon
Control
Flow
Entry-controlled
Entry-
controlled
Jumps outside
the loop
Jumps to next
iteraon
Flexibility
High, but requires
manual updates
Compact and
structured
Used for early
exit
Used to skip
some condions
Example
Use
Wait for user input
Print 1 to 10
numbers
Stop loop when
error occurs
Ignore invalid
data in loop
󹰤󹰥󹰦󹰧󹰨 Pro Tips for Programmers
Use for when you know how many mes you want to loop.
Use while when you don’t know how long the loop should run.
Use break to escape from a loop when something crical happens.
Use connue to skip something without aecng the rest of the loop.
Easy2Siksha.com
󷓠󷓡󷓢󷓣󷓤󷓥󷓨󷓩󷓪󷓫󷓦󷓧󷓬 Conclusion
Just like Anu and Rahul, every programmer must choose their tools wisely. The while and for
loops are like two paths leading to the same desnaon — they both help us repeat code.
Similarly, break and connue give us powerful control over our loops, helping us make smart
decisions inside them.
By understanding the subtle dierences and praccing them with real examples, anyone can
master these fundamental concepts of C.
4. Write a program to check if a number given by user is Palindrome or not.
Ans: Imagine you're holding a magical mirror. This mirror doesn’t show your reecon;
instead, it ips numbers. Show it 121, and it reects back 121. Show it 123, and it returns
321. Now, here's the twist — if a number and its reecon in this mirror are the same, its a
Palindrome!
In simpler words, a palindrome number is one that remains the same when its digits are
reversed. Examples include 121, 1331, 44444 — these numbers are like calm lakes, sll and
symmetrical from both ends.
Now, lets take this idea from the imaginaon world and bring it to life through
programming.
󼨐󼨑󼨒 Before the Code – Understand the Logic
A number is a palindrome if:
Its reverse is equal to the original number.
So, our task is:
1. Take a number from the user.
2. Reverse the digits of the number.
3. Compare the reversed number to the original number.
4. If both are the same, it’s a palindrome. Otherwise, its not.
Lets now understand this concept using a short and simple story — just like you'd tell a child
while explaining a math problem.
󼬰󼬮󼬯 Short Story: The Kingdom of Numbers
Easy2Siksha.com
Long ago in the Kingdom of Numbers, King Palindro ruled with symmetry and order. His rule
was simple: "Only those numbers that look the same from front and back shall enter the
Royal Palace." One day, a curious number, 12321, knocked at the palace gates.
"Let me in!" cried 12321.
The gatekeeper reversed the number — starng from the end, reading it as 1-2-3-2-1 — and
found it to be exactly the same.
"You may enter!" said the gatekeeper with a smile.
Another number, 12345, tried to enter, but its reversed form was 54321 — not the same.
"Sorry, your reecon does not match. You cannot enter," replied the gatekeeper.
Thus, the rule of palindrome kept the kingdom perfectly balanced and beauful.
󹰤󹰥󹰦󹰧󹰨 How Do We Write a Program to Do This?
Lets take this beauful concept and turn it into a working Python program.
# Palindrome Number Checker Program
# Step 1: Ask the user to input a number
num = int(input("Enter a number: "))
# Step 2: Store the original number
original_num = num
# Step 3: Reverse the number
reversed_num = 0
while num > 0:
digit = num % 10 # Extract the last digit
reversed_num = reversed_num * 10 + digit # Append the digit
num = num // 10 # Remove the last digit
# Step 4: Check if the original and reversed numbers are the same
if original_num == reversed_num:
Easy2Siksha.com
print("Yes! It is a Palindrome number.")
else:
print("No! It is not a Palindrome number.")
󹸯󹸭󹸮 Let's Break It Down Like a Detecve
Take the input 121:
First digit (from the end): 1
Next: 2
Next: 1
We build the reversed number as:
Start with 0 → 0 * 10 + 1 = 1
1 * 10 + 2 = 12
12 * 10 + 1 = 121
And voila! We get back the original number — hence, Palindrome!
󹴡󹴵󹴣󹴤 Another Short Real-Life Example
Think about a school quiz. You are asked: “Can you think of numbers that read the same
forward and backward?”
You smile and say: "Yes! 121, 909, 1221..."
Now think: wouldn’t it be amazing if your computer could do this too? Thats what our lile
Python code is doing — becoming your intelligent assistant in the world of numbers.
󷃆󹸊󹸋 Quick Glance at the Steps Again
Step
What Happens
1
Take user input
2
Store the original number
3
Reverse the digits using a loop
4
Compare reversed number with original
Easy2Siksha.com
Step
What Happens
5
Print the result based on comparison
󷕘󷕙󷕚 Why This is More Than Just a Program
Checking for a palindrome teaches us:
How to manipulate numbers
How to use loops and condions
How to break problems into simple steps
Its like teaching your computer how to “look in the mirror— a fantasc introducon to
logical thinking!
󺫦󺫤󺫥󺫧 Want to Try a Shorter Version Using Python Tricks?
Heres a one-liner magic trick using Python:
num = input("Enter a number: ")
print("Palindrome!" if num == num[::-1] else "Not a Palindrome!")
Here, num[::-1] ips the string. But remember, this works only because we treat the number
as a string.
󷙎󷙐󷙏 Conclusion
So, in the magical world of programming, palindrome numbers are like royal guests who can
look the same in both direcons. With just a few lines of code, we’ve made a mirror that
reects any number and checks if it’s worthy of entering King Palindro’s palace!
Whether its 121, 4444, or even 99999, you now know how to spot a palindrome — and
more importantly, how to teach your computer to spot it too!
And thats the real beauty of learning to code — turning stories into soluons. 󷇴󷇵󷇶󷇷󷇸󷇹
Easy2Siksha.com
SECTION-C
5. (a) What is a string? Explain any ve string manipulaon funcons.
(b) Write a program to get a string from user and count number of vowels in it.
Ans: (a) Once upon a me in the Kingdom of Data...
In the magical land of Computeria, where data lived in peace, there were many dierent
types of data cizens: numbers, decimals, booleans, and one of the most expressive of them
all — Strings.
Among all data types, String was the most talkave. While numbers could only count, and
booleans could only say "true" or "false", strings could speak, write names, compose
sentences, and even form stories. If you ever typed a name like "Rishabh", a sentence like
"Welcome to Easy2Siksha", or even a message like "Error: Page not found", you were dealing
with strings.
So, what exactly is a string?
󼨻󼨼 Denion of a String:
In programming, a string is a sequence of characters leers, numbers, symbols, or spaces
— enclosed in quotaon marks (single ' ' or double " ").
Examples:
"Hello" – A simple string
"1234" – Even though it looks like a number, its a string if in quotes
"Welcome to Easy2Siksha" – A string with spaces
In simple words, a string is anything you can type on your keyboard and put inside quotes.
That could be a name, address, sentence, code, or even a paragraph!
Now that you’ve met our character “String,” lets explore ve magical powers or string
manipulaon funcons that help us work with strings in programming.
These funcons are like tools in a wizard’s kit, used to twist, shape, analyze, and transform
strings into whatever form we need!
󺫦󺫤󺫥󺫧 1. length() – The Measuring Tape
Once upon a class in Easy2Siksha Academy, a student asked,
“How long is my string?
Easy2Siksha.com
Thats when the length() funcon came to the rescue.
󹴡󹴵󹴣󹴤 Purpose: It tells us how many characters are in a string (including spaces and symbols).
󹳴󹳵󹳶󹳷 Example:
text = "Hello World"
print(len(text)) # Output: 11
󼨐󼨑󼨒 Even the space between “Hello” and “World” is counted! This funcon is helpful when
you want to check passwords, usernames, or message lengths.
󺫦󺫤󺫥󺫧 2. lower() and upper() – The Voice Changers
Lets imagine a situaon. You are lling out a form that asks your name in capital leers, but
you typed it in small leers.
These two funcons help:
upper() makes everything UPPERCASE
lower() makes everything lowercase
󹳴󹳵󹳶󹳷 Example:
name = "Rishabh"
print(name.upper()) # Output: RISHABH
print(name.lower()) # Output: rishabh
󼨐󼨑󼨒 These are useful for case-insensive comparisons, formang text for display, or even for
shoung in a message (just kidding — but it works!).
󺫦󺫤󺫥󺫧 3. replace() – The Quick Switcher
This funcon is like a magician who can change words instantly.
Let’s say you wrote:
"I love Python"
But you now want it to say:
"I love Java"
Just use the replace() funcon!
󹳴󹳵󹳶󹳷 Example:
sentence = "I love Python"
print(sentence.replace("Python", "Java")) # Output: I love Java
Easy2Siksha.com
󼨐󼨑󼨒 Its useful when correcng text, replacing sensive informaon, or even eding mulple
sentences in a chatbot!
󺫦󺫤󺫥󺫧 4. strip() – The Cleaner
Once in Computeria, a string walked into a database with muddy shoes — that is, extra
spaces before or aer it!
Thats when the strip() funcon politely cleaned it up.
󹳴󹳵󹳶󹳷 Example:
word = " Easy2Siksha "
print(word.strip()) # Output: Easy2Siksha
󼨐󼨑󼨒 strip() removes extra spaces from the beginning and end of a string. There’s also lstrip()
(le strip) and rstrip() (right strip).
This is especially useful in data entry, forms, and when you're taking input from users.
󺫦󺫤󺫥󺫧 5. split() – The String Slicer
Here’s another tale.
A teacher received a string from a student:
"apple,banana,grape"
But the teacher wanted to grade each fruit separately. So, she used split() — the slicer
funcon that turns a string into a list of items.
󹳴󹳵󹳶󹳷 Example:
fruits = "apple,banana,grape"
print(fruits.split(",")) # Output: ['apple', 'banana', 'grape']
󼨐󼨑󼨒 Its helpful in reading CSV les, breaking sentences into words, or separang data for
processing.
󽄻󽄼󽄽 Why Are These Funcons Important?
Think of a string like a sentence in real life — we oen:
Count the number of words (like length)
Change tone (like upper/lower)
Replace words
Easy2Siksha.com
Clean up spacing
Break it apart
These are the same things we do with strings in programming. Thats why string
manipulaon funcons are taught early — they are simple but powerful tools that help you
control and format the way data is shown and handled.
󼪀󼪃󼪄󼪁󼪅󼪆󼪂󼪇 Final Wrap-Up (in plain words):
A string is just text inside quotaon marks.
It can be manipulated using many built-in funcons in programming.
These funcons save me, avoid errors, and make your code smart and clean.
󷙎󷙐󷙏 Summary Table:
Funcon
Purpose
Example Output
len()
Count characters
"Hello" → 5
upper()
Convert to uppercase
"rishabh" → "RISHABH"
replace()
Replace a part of the string
"hi" → "hello"
strip()
Remove extra spaces
" Easy2Siksha " → "Easy2Siksha"
split()
Break string into parts
"a,b,c" → ['a', 'b', 'c']
So next me you see a line of text on a screen, remember — its a string that might have
been measured, cleaned, capitalized, or split, thanks to these magical funcons.
(b) Write a program to get a string from user and count number of vowels in it.
Ans: Once Upon a Time in the Kingdom of Code...
In a magical land where computers and codes lived like friends, there was a curious young
student named Anya. She had just started learning programming and was fascinated by how
words and leers could talk to a machine. One day, her mentor gave her a challenge:
"Dear Anya, can you write a small program that takes a sentence from the user and counts
how many vowels are hidden inside it?"
Anya smiled, ready for the challenge. But before wring the code, she decided to
understand everything step by step, like solving a fun mystery.
Easy2Siksha.com
Step 1: Understanding the Problem Like a Detecve
When Anya looked at the queson, she realized it’s not just about wring code — its about
telling the computer how to think. So she broke it down:
The computer must ask the user to enter a sentence or a string.
Then, it must look at each leer of that string, one by one.
Whenever it nds a vowel (like a, e, i, o, u), it should count it.
At the end, it should display the total number of vowels found.
Step 2: The Vowel Team
Anya remembered a story her teacher once told:
The vowels – A, E, I, O, U – were like superheroes of the English language. They appeared in
almost every word to make it complete. So, in the code, Anya had to look for these leers,
both in lowercase and uppercase (because 'A' and 'a' are both vowels).
So, the superhero team would be:
a, e, i, o, u, A, E, I, O, U
Step 3: The Code
Now that she had the plan, Anya wrote the following code in Python:
# Get input from the user
user_input = input("Enter a string: ")
# Dene vowels
vowels = "aeiouAEIOU"
# Inialize counter
vowel_count = 0
# Loop through each character in the string
for char in user_input:
if char in vowels:
vowel_count += 1
Easy2Siksha.com
# Display the result
print("Number of vowels:", vowel_count)
Step 4: Explaining the Code Like a Story
Lets now walk through what this code is doing, just like Anya would explain it to her lile
brother:
1. input("Enter a string: ")
The computer is asking the user politely: "Please tell me a sentence."
2. vowels = "aeiouAEIOU"
This line tells the computer: "Hey! Here’s the list of heroes we are searching for!"
3. vowel_count = 0
We start with zero because we haven’t found any vowels yet.
4. for char in user_input:
The computer goes on a leer-by-leer adventure inside the sentence.
5. if char in vowels:
At each step, it checks: "Is this leer a vowel?"
6. vowel_count += 1
If yes, it proudly says: "Aha! Found one!" and increases the count.
7. print("Number of vowels:", vowel_count)
Finally, it tells the user how many vowels it found.
Why This Code is Smart and Simple
It works with any kind of sentence – short, long, uppercase, or lowercase.
It avoids complicated logic – just a simple loop and a condion.
The use of vowels = "aeiouAEIOU" makes it clean and readable.
A Second Story: The Classroom Surprise
A few days later, during a coding compeon in her school, Anya’s teacher gave the same
queson to everyone. While others panicked, Anya smiled. She had already cracked this
puzzle once.
She typed her program condently, and guess what? She even helped others understand it
using the same superhero story of vowels. The teacher was so impressed that he said:
Easy2Siksha.com
"Anya, you didn’t just solve the problem – you taught it like a true coder."
Moral of the Story
Programming isn’t just about wring code – its about understanding the logic, simplifying it,
and explaining it like a story so that even a beginner can enjoy the journey.
So next me you write a program, remember Anya’s steps:
Understand the problem
Break it into simple tasks
Write readable code
And most importantly, make it fun!
6. What is Recursion? Write a program to nd factorial of a given number using recursive
funcon.
Ans: 󷇴󷇵󷇶󷇷󷇸󷇹 Once Upon a Time in a Village of Mathemagicians...
In a quiet village where numbers came alive, there lived a wise old Mathemagician named
Rishi. He was known for solving the biggest problems with the smallest steps. One day, a
curious lile boy named Arjun came to him and asked:
“Master Rishi, I heard people talking about this magical thing called recursion. What is it?”
The old man chuckled and said,
Ah, recursion! It’s like asking someone to do a job, and that someone says — ‘Sure! But
rst, let me ask another person to do a smaller part of it for me. When they’re done, I’ll
complete my part too!’”
Arjun was confused. So Rishi explained it with a real-life example.
󷏙󷏚󷏛󷏜 Story of the Magical Mirror
“Imagine you stand in front of two mirrors facing each other,” said Rishi. “You see an image
of yourself, then another image behind that, and then another… and it goes on and on unl
it becomes too ny to see. Thats how recursion works in programming — a funcon keeps
calling itself again and again with smaller inputs, unl it reaches a point where it stops —
known as the base case.”
Now Arjun was geng interested!
Rishi connued,
Easy2Siksha.com
“Let me show you a magic trick with numbers — the factorial. You know what 5 factorial
means, right?”
Arjun replied,
Yes! 5! = 5 × 4 × 3 × 2 × 1 = 120.
Rishi smiled.
“Exactly! But instead of mulplying all the numbers manually, what if we made a funcon
that solves it step-by-step using recursion? Let me show you how.
󼨐󼨑󼨒 Understanding Factorial Recursively
Let’s break it down:
5! = 5 × 4!
4! = 4 × 3!
3! = 3 × 2!
2! = 2 × 1!
1! = 1 (this is the base case — the smallest problem that we can solve directly)
So, to solve 5!, we just need to solve 4!, and to solve 4!, we need 3!, and so on. This chain
connues unl we reach 1, and then everything starts resolving backward, just like climbing
down a ladder and then back up.
This is recursion in acon — solving a big problem by breaking it into smaller copies of the
same problem!
󹲙󹲚󹲛󹲜󹲝󹲞 Now, Let’s Write the Magic (C Program)
#include <stdio.h>
// Recursive funcon to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1) {
// Base case: factorial of 0 or 1 is 1
return 1;
} else {
// Recursive case: n * factorial of (n-1)
Easy2Siksha.com
return n * factorial(n - 1);
}
}
int main() {
int number;
prin("Enter a number to nd its factorial: ");
scanf("%d", &number);
if (number < 0) {
prin("Factorial is not dened for negave numbers.\n");
} else {
prin("Factorial of %d is %d\n", number, factorial(number));
}
return 0;
}
󼨻󼨼 Explanaon of the Code Like a Story
Lets imagine the funcon factorial(n) as a character named Funky Factorial, who receives a
number n and has a job to nd its factorial.
If n is 1 or 0, Funky says:
“Easy peasy! The factorial is just 1. I’m done!”
But if n is more than 1, Funky says:
“Hmm, too big! I’ll ask my younger brother Funky(n - 1) to do it for me, and I’ll mulply the
result by n.
This connues down unl Funky(1) says,
“I’ve got this! Returning 1!”
Then, like magic, each Funky funcon passes the result back up:
Easy2Siksha.com
Funky(2) returns 2 × 1 = 2
Funky(3) returns 3 × 2 = 6
Funky(4) returns 4 × 6 = 24
Funky(5) returns 5 × 24 = 120
And just like that, the factorial of 5 is found through recursion!
󼨐󼨑󼨒 Why is Recursion Special?
Recursion is like a superpower in programming. It helps us solve complex problems in a neat,
elegant way, especially when the problem can be broken down into similar smaller problems
— like tree structures, puzzles, or mathemacal series.
But just like magic, recursion should be used carefully. Without a base case, it would go on
forever — like falling into an endless mirror loop!
󽄻󽄼󽄽 Final Words
So, whenever you hear the word recursion, think of a team of helpers, each solving a smaller
task and passing the answer back — like a relay race. And when used right, it makes
problems like factorials, Fibonacci numbers, tower of Hanoi, and more look like childs play!
Next me you see a big problem, don’t panic. Just ask yourself,
“Can I solve a smaller version of this and build up to the full answer?”
Thats the recursive waysmall steps, big soluons.
SECTION-D
7. What is structure in C? Which are types of structures ? Explain the uses of structures:
Give an example.
Ans: In that classroom, there’s a teacher named Mr. Sharma, who is known for his unique
way of taking aendance. Instead of calling names and checking roll numbers from a
register, he maintains a separate notebook for each student with their name, roll number,
marks, and grade. Every me he checks aendance, he opens a student’s notebook and sees
all the informaon together.
Now imagine if Mr. Sharma had to keep four dierent registers — one for names, one for roll
numbers, one for marks, and one for grades. That would be confusing, right?
This is exactly what structures in C solve.
Easy2Siksha.com
So, What is a Structure in C?
In C programming, a structure (or struct) is a user-dened data type that allows you to
combine dierent types of data items together under one name.
While arrays can hold mulple values of the same type, structures can hold values of
dierent types. That’s the beauty of it.
So, instead of storing a students name (string), roll number (int), marks (oat), and grade
(char) in dierent variables, you can put them all together in one unit — a structure.
Think of a structure like a custom data container or a mini database record. It groups related
informaon, just like a folder holds dierent papers about the same student.
󼨻󼨼 Why Do We Need Structures?
Let’s take a very small example:
char name[20];
int rollNo;
oat marks;
char grade;
This is ne if you are dealing with just one student. But what if there are 50 students?
You’ll end up having 4 separate arrays, and you’ll have to remember that name[3], rollNo[3],
marks[3], and grade[3] all belong to the same student. Thats messy.
But using structures, you can dene a student blueprint and then create 50 student records
easily.
󷧺󷧻󷧼󷧽󷨀󷧾󷧿 Dening a Structure
Heres how we dene a structure in C:
struct Student {
char name[20];
int rollNo;
oat marks;
char grade;
};
Easy2Siksha.com
Here, Student is a structure type that holds four dierent pieces of data — each of a
dierent type.
You can now create a variable of type struct Student like this:
struct Student s1;
This s1 can now hold all the data related to one student.
󼨻󼨼 Types of Structures in C
In C, structures can be used in dierent ways, leading to dierent types or usage paerns:
1. Simple Structure
This is the most basic form, like the one shown above. It holds dierent data types together.
2. Array of Structures
You can create an array where each element is a structure.
struct Student students[50];
This is perfect for handling mulple records.
3. Nested Structures
A structure can contain another structure as its member.
struct Date {
int day, month, year;
};
struct Student {
char name[20];
int rollNo;
struct Date dob; // Date of Birth
};
This shows how structures can be nested to represent more complex informaon.
4. Pointer to Structure
You can also create pointers to structures and dynamically allocate memory using malloc.
struct Student *ptr;
Easy2Siksha.com
ptr = (struct Student*) malloc(sizeof(struct Student));
Useful for handling data in heap memory or with linked lists.
󷗭󷗨󷗩󷗪󷗫󷗬 Uses of Structures in C
Structures are extremely useful, especially when you are dealing with real-life enes or
complex data. Here are some key uses:
󷃆󼽢 1. Handling Real-World Data
Structures help model real-world enes — like students, employees, books, customers, etc.
󷃆󼽢 2. Creang Complex Data Structures
Structures are the foundaon of more advanced data structures like linked lists, trees,
stacks, queues, etc.
󷃆󼽢 3. Funcon Argument Grouping
Instead of passing mulple variables to a funcon, you can pass one structure that contains
all necessary data.
󷃆󼽢 4. File Handling
When saving data to les (like saving student records), structures help you read and write an
enre record at once.
󷃆󼽢 5. Modular Programming
Structures help organize code beer by grouping related variables, which makes large
programs easier to manage and debug.
󹴡󹴵󹴣󹴤 Example with Explanaon
Lets write a simple C program using structures.
#include <stdio.h>
struct Student {
char name[20];
int rollNo;
oat marks;
char grade;
Easy2Siksha.com
};
int main() {
struct Student s1 = {"Ravi", 101, 89.5, 'A'};
prin("Student Informaon:\n");
prin("Name: %s\n", s1.name);
prin("Roll No: %d\n", s1.rollNo);
prin("Marks: %.2f\n", s1.marks);
prin("Grade: %c\n", s1.grade);
return 0;
}
󹲹󹲺󹲻󹲼󹵉󹵊󹵋󹵌󹵍 Output:
Student Informaon:
Name: Ravi
Roll No: 101
Marks: 89.50
Grade: A
This simple program creates a structure Student, stores the informaon of a student named
"Ravi", and then prints it.
󹴮󹴯󹴰󹴱󹴲󹴳 A Short Story: The Library System
Imagine you are designing a library system. Each book has:
A tle (string),
An author (string),
A book ID (integer),
A price (oat)
If you try to manage each piece of informaon in dierent arrays, it becomes very hard to
match book IDs with tles and authors.
Easy2Siksha.com
But with a structure like this:
struct Book {
char tle[50];
char author[50];
int bookID;
oat price;
};
Now you can easily manage an array of books. You can store, search, and update informaon
easily. Structures make your program look organized, logical, and closer to real life.
󷃆󼽢 Conclusion
A structure in C is a powerful feature that allows you to create your own data types by
combining variables of dierent types under one name. It is like a container or record that
keeps all related informaon together.
From students and employees to books and bank accounts, structures help us model real-
world objects in programming. They are also the foundaon of more complex systems.
In short:
Structures make code clean, organized, and real-life-oriented.
They are easy to use, exible, and essenal for data management in C.
So next me you see a messy collecon of related variables, think like Mr. Sharma and put
them all in a neat structure! 󺅕󺅓󺅖󺅗󺅘󺅔
8. What is a pointer ? How are pointers helpful in accessing an array ? Explain
call by reference.
Ans: In a small digital kingdom inside a computer, there lived a smart lile postman named
Pointer. Unlike normal messengers, Pointer didn't carry any leers or packages. Instead, he
carried something even more powerful—addresses.
You see, every house (memory locaon) in the kingdom had a unique number—just like real-
world addresses. All the data (like numbers, characters, arrays, etc.) lived in these houses.
So, whenever someone wanted to nd a piece of data, they would ask Pointer, because he
knew exactly where it was located.
Lets now break down Pointers story and how he helps us in programming.
Easy2Siksha.com
󷃆󼽢 What is a Pointer?
In simple terms, a pointer is a variable that stores the memory address of another variable.
Let’s take an example:
int num = 10;
int *ptr = &num;
Here, num is a normal variable holding the value 10.
&num means “address of num”.
ptr is a pointer to an integer, and it's storing the address of num.
So now, ptr doesn't store 10. It stores the locaon where 10 is kept. And using that locaon,
it can indirectly access or modify the value of num.
󷃆󼽢 Why are Pointers Useful?
You might wonder, “Why not just use num directly?” Great queson! The power of pointers
comes when we want to deal with arrays, funcons, memory management, or large data
structures. Lets look at how Pointer helps with arrays.
󼪟󼪠󼪡 How are Pointers Helpful in Accessing an Array?
Imagine an array is like a row of houses, each with a number (index) and each holding a
value (like books in shelves).
int arr[5] = {10, 20, 30, 40, 50};
In memory, these values are stored side by side. The pointer to the rst element of the array
is:
int *ptr = arr; // or int *ptr = &arr[0];
Now comes the magic! Just by using the pointer ptr, we can move from one house to
another.
*(ptr + 0) = 10
*(ptr + 1) = 20
*(ptr + 2) = 30
So, instead of wring arr[2], we can write *(ptr + 2) to get the same value!
Pointers allow us to navigate through arrays easily without using index notaon. This is
extremely useful in cases like:
Easy2Siksha.com
Passing arrays to funcons
Dynamic memory allocaon
Iterang through data eciently
󼨐󼨑󼨒 Call by Reference – The Real Power of Pointers
Lets now understand call by reference, a concept that makes pointers even more magical.
󹴮󹴯󹴰󹴱󹴲󹴳 Mini Story: The Chef and the Recipe
There was once a famous chef named Maya. She had a student named Rohan who always
wanted to improve her recipes. One day, Rohan asked Maya for her original recipe book.
Maya was smart and didn't give him the whole book. Instead, she gave him a copy. Rohan
made changes in the copy, but Maya’s original recipe remained untouched.
This is like call by value—we pass a copy of the variable to a funcon, so changes inside the
funcon don’t aect the original variable.
But what if Maya gave Rohan the address of her original recipe book? Now, whatever Rohan
changes will directly aect the original! This is call by reference.
󼨽󼨾󼨿󼩁󼩀 Technical Explanaon
In call by reference, we pass the address of a variable to a funcon, and any changes made
inside the funcon are reected in the original variable.
Example:
void update(int *p) {
*p = *p + 10;
}
int main() {
int num = 20;
update(&num);
cout << num; // Output: 30
}
Heres what happens:
Easy2Siksha.com
1. num is 20.
2. update(&num) sends the address of num to the funcon.
3. Inside the funcon, *p means “go to that address and update the value”.
4. So, the original num becomes 30.
This is very helpful when we want a funcon to modify original data, save memory, or deal
with large objects (like arrays, strings, etc.) eciently.
󷃆󼽢 Why Do Examiners Love Answers with Pointers?
Because they show that the student:
Understands memory concepts.
Can write ecient programs.
Knows how data moves inside a computer.
Can use powerful techniques like dynamic memory, arrays, structures, and recursion.
󹲹󹲺󹲻󹲼󹵉󹵊󹵋󹵌󹵍 Conclusion
To sum up:
A pointer is a variable that stores the address of another variable.
Pointers are helpful in accessing arrays by navigang through memory addresses
instead of indices.
In call by reference, we use pointers to allow funcons to modify the original values,
making code more ecient and powerful.
Whether it's the postman in the digital kingdom or the chef who gave away her recipe’s
address—pointers are like magical keys that unlock the doors of real power in programming!
This paper has been carefully prepared for educaonal purposes. If you noce any mistakes or
have suggesons, feel free to share your feedback.